﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.Swagger;
using Tdb.YY.API.Filters;

namespace Tdb.YY.API
{
    public class Startup
    {
        /// <summary>
        /// 配置
        /// </summary>
        public IConfiguration Configuration { get; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configuration"></param>
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            //添加过滤器
            var mvcBuilder = services.AddMvc(AddFilters);
            mvcBuilder.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            //压缩
            services.AddResponseCompression();
            //缓存
            services.AddResponseCaching();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v4", new Info { Title = "YY系统接口", Version = "v1" });

                //接口注释
                var xmlAPI = Path.Combine(AppContext.BaseDirectory, "Tdb.YY.API.xml");
                c.IncludeXmlComments(xmlAPI);

                //参数注释
                var xmlDTO = Path.Combine(AppContext.BaseDirectory, "Tdb.YY.DTO.xml");
                c.IncludeXmlComments(xmlDTO);

                //生成token输入框
                c.OperationFilter<SwaggerOperationFilter>();
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            //配置跨越
            app.UseCors(builder =>
            {
                builder.AllowAnyHeader();
                builder.AllowAnyMethod();
                builder.SetPreflightMaxAge(TimeSpan.FromDays(1));

                //builder.WithOrigins(IOSysJson.Inst.AppConfig.LstWithOrigins.ToArray());
                builder.AllowAnyOrigin();

            });

            //压缩
            app.UseResponseCompression();
            //缓存
            app.UseResponseCaching();

            // Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger();

            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), 
            // specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v4/swagger.json", "YY系统接口 V1");
            });

            app.UseMvc();
        }

        /// <summary>
        /// 添加过滤器
        /// </summary>
        /// <param name="option">配置</param>
        private void AddFilters(MvcOptions option)
        {
            //验权过滤器
            option.Filters.Add(typeof(AuthFilterAttribute));
            //异常过滤器
            option.Filters.Add(typeof(ExcpFilterAttribute));
        }
    }
}
